package com.sc.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.springboot.pojo.Notification;
import com.sc.springboot.pojo.Users;
import com.sc.springboot.service.INotificationService;
import com.sc.springboot.util.JwtUtils;
import com.sc.springboot.util.ResponseMsg;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author romeo
 * @since 2025-09-22 02:30:52
 */
@RestController
@RequestMapping("/notifications")
public class NotificationController {

    @Autowired
    private INotificationService notificationService;

    //根据用户id查询通知列表并分页
    @RequestMapping("/getByUid")
    public ResponseMsg getByUid(HttpServletRequest request) {
        //1.解析token
        String token = request.getHeader("token");
        System.out.println("我在这"+token);
        try {
            /*Claims claims = JwtUtils.parseJWT(token);
            System.out.println("claims:"+claims.getSubject());
            Users user = JSON.parseObject(claims.getSubject(), Users.class);
            System.out.println("user:"+user);
            if (!JwtUtils.isAvailable(user.getUsername())){
                throw new Exception("服务端token错误");
            }
            Integer uid=user.getUserId();
            System.out.println("uid:"+uid);*/
            List<Notification> notifications = notificationService.getByUid(2);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, notifications);
        } catch (Exception e) {//1.格式错误2.过期3.签名错误
            System.out.println("jwtToken验证失败,错误信息"+e.getMessage());
            return null;
        }
    }

    //根据通知id标记该通知为已读
    @RequestMapping("/markAsRead")
    public ResponseMsg markAsRead(@RequestParam Integer notiId) {
        Notification notification = notificationService.getById(notiId);
        notification.setIsRead(true);
        notificationService.updateById(notification);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "标记成功");
    }

    //根据用户id标记该用户的所有通知为已读
    @RequestMapping("/markAllAsRead")
    public ResponseMsg markAllAsRead(@RequestParam Integer userId) {
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Notification> list = notificationService.list(queryWrapper);
        for (Notification notification : list) {
            notification.setIsRead(true);
            notificationService.updateById(notification);
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "标记成功");
    }
    //根据通知id删除通知
    @RequestMapping("/delete")
    public ResponseMsg delete(@RequestParam Integer notiId) {
        notificationService.removeById(notiId);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "删除成功");
    }
    //根据通知id查询通知详情
    @RequestMapping("/getDetail")
    public ResponseMsg getById(@RequestParam Integer notiId) {
        Notification notification = notificationService.getById(notiId);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, notification);
    }
}
